﻿using System;
using System.Data;
using System.Data.SqlServerCe;

namespace EOS.Data.Utils
{
    public class SqlceConnectionPool
    {
        [ThreadStatic]
        private IDbConnection _threadConnection;

        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <value>The connection string.</value>
        public string ConnectionString
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets a connection.
        /// </summary>
        /// <returns>An open connection</returns>
        public IDbConnection Connection
        {
            get
            {
                if (_threadConnection == null)
                {
                    _threadConnection = new SqlCeConnection(ConnectionString);
                    _threadConnection.Open();
                }

                return _threadConnection;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SqlceConnectionPool"/> class.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        public SqlceConnectionPool(string connectionString)
        {
            ConnectionString = connectionString;
        }

    }
}
